<?php

/**
 * @Project NUKEVIET 3.0
 * @Author VINADES.,JSC (contact@vinades.vn)
 * @Copyright (C) 2010 VINADES., JSC. All rights reserved
 * @Createdate 3/25/2010 18:6
 */
if (!defined('NV_SYSTEM'))
    die('Stop!!!');
if(!defined('CP_DOC_SEARCH_LOADED')){
    define('CP_DOC_SEARCH_LOADED', 1);
}
else{
    exit();
}
/**
 * nv_setcats()
 *
 * @param mixed $list2
 * @param mixed $id
 * @param mixed $list
 * @param integer $m
 * @param integer $num
 * @return
 */
if(!function_exists("setSubCats")){
    function setSubCats( $list2, $id, $list, $m = 0, $num = 0 )
    {
        $num++;
        $defis = "";
        for ( $i = 0; $i < $num; $i++ )
        {
            $defis .= "--";
        }

        if ( isset( $list[$id] ) )
        {
            foreach ( $list[$id] as $value )
            {
                if ( $value['id'] != $m )
                {
                    $list2[$value['id']] = $value;
                    $list2[$value['id']]['name'] = "|" . $defis . "&gt; " . $list2[$value['id']]['name'];
                    if ( isset( $list[$value['id']] ) )
                    {
                        $list2 = setSubCats( $list2, $value['id'], $list, $m, $num );
                    }
                }
            }
        }
        return $list2;
    }
}
/**
 * nv_listcats()
 *
 * @param mixed $parentid
 * @param integer $m
 * @return
 */
if(!function_exists("getAllCats")){
    function getAllCats( $parentid, $m = 0 )
    {
        global $db;

        $sql = "SELECT * FROM `" . NV_PREFIXLANG . "_download_categories` WHERE `status`=1 ORDER BY `parentid`,`weight` ASC";
        $result = $db->sql_query( $sql );
        $list = array();
        while ( $row = $db->sql_fetchrow( $result ) )
        {
            $list[$row['parentid']][] = array( //
                'id' => ( int )$row['id'], //
                'parentid' => ( int )$row['parentid'], //
                'title' => $row['title'], //
                'alias' => $row['alias'], //
                'description' => $row['description'], //
                'who_view' => ( int )$row['who_view'], //
                'groups_view' => ! empty( $row['groups_view'] ) ? explode( ",", $row['groups_view'] ) : array(), //
                'who_download' => ( int )$row['who_download'], //
                'groups_download' => ! empty( $row['groups_download'] ) ? explode( ",", $row['groups_download'] ) : array(), //
                'weight' => ( int )$row['weight'], //
                'status' => $row['weight'], //
                'name' => $row['title'], //
                'selected' => $parentid == $row['id'] ? " selected=\"selected\"" : "" //
                );
        }

        if ( empty( $list ) )
        {
            return $list;
        }

        $list2 = array();
        foreach ( $list[0] as $value )
        {
            if ( $value['id'] != $m )
            {
                $list2[$value['id']] = $value;
                if ( isset( $list[$value['id']] ) )
                {
                    $list2 = setSubCats( $list2, $value['id'], $list, $m );
                }
            }
        }

        return $list2;
    }
}

global $client_info, $global_config, $module_name, $module_info, $lang_global;
global $module_name, $lang_module, $module_data, $nv_Request;
if (file_exists(NV_ROOTDIR . "/themes/" . $global_config['module_theme'] . "/blocks/global.doc_search.tpl")) {
    $block_theme = $global_config['module_theme'];
} elseif (file_exists(NV_ROOTDIR . "/themes/" . $global_config['site_theme'] . "/blocks/global.doc_search.tpl")) {
    $block_theme = $global_config['site_theme'];
} else {
    $block_theme = "default";
}
$xtpl = new XTemplate("global.doc_search.tpl", NV_ROOTDIR . "/themes/" . $block_theme . "/blocks");

$xtpl->assign( 'LANG', $lang_global );
$key = filter_text_input( 'q', 'post', '', 1, NV_MAX_SEARCH_LENGTH );
$cat_i = $nv_Request->get_int( 'cat', 'post' );
$xtpl->assign( 'keyvalue', $key );
$xtpl->assign( 'FORMACTION', NV_BASE_SITEURL . 'index.php?' . NV_LANG_VARIABLE . '=' . NV_LANG_DATA . '&' . NV_NAME_VARIABLE . '=download&' . NV_OP_VARIABLE . '=newsearch' );
$nv_list_cats = getAllCats(0);
foreach ( $nv_list_cats as $cat )
{
    if($cat_i == $cat['id'] ){
        $cat['selected'] = "selected=\"selected\"";
    }
    $xtpl->assign( 'LISTCATS', $cat );
    $xtpl->parse( 'main.catid' );
}


$xtpl->parse( 'main' );
$content = $xtpl->text( 'main' );

?>